package com.yanqu.road.server.cmd.activity.hsisland;

import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.HsIslandProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.hsisland.CrossHsIslandActivity;
import com.yanqu.road.server.manager.activity.hsisland.CrossHsIslandActivityMgr;
import com.yanqu.road.server.manager.activity.hsisland.CrossHsIslandUser;
import com.yanqu.road.server.manager.activity.hsisland.pb.CrossHsIslandActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_HS_ISLAND_USE_PROP_RECOVER_ENERGY, desc = "海神岛-使用道具恢复体力")
public class CrossHsIslandUsePropRecoverEnergyCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        HsIslandProto.HsIslandUsePropRecoverEnergyReqMsg reqMsg = HsIslandProto.HsIslandUsePropRecoverEnergyReqMsg.parseFrom(packet.getBytes());
        HsIslandProto.HsIslandUsePropRecoverEnergyRespMsg.Builder respMsg = HsIslandProto.HsIslandUsePropRecoverEnergyRespMsg.newBuilder();
        respMsg.setActivityId(reqMsg.getActivityId());
        respMsg.setNum(reqMsg.getNum());
        CrossHsIslandActivity activity = CrossHsIslandActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_HS_ISLAND_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_HS_ISLAND_USE_PROP_RECOVER_ENERGY_FROM_CROSS, respMsg));
            return;
        }
        try {
            //使用道具恢复体力
            activity.usePropRecoverEnergy(packet.getPlayerId(), reqMsg.getNum());
            //构造PB
            CrossHsIslandUser user = activity.getUser(packet.getPlayerId());
            respMsg.setRet(0);
            respMsg.setUserData(CrossHsIslandActivityPb.buildHsIslandUserData(user));
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_HS_ISLAND_USE_PROP_RECOVER_ENERGY_FROM_CROSS, respMsg));
    }

    @Override
    public void sendData() {}
}
